package com.demo.seckill.queue.redis;

import com.demo.seckill.common.entity.Result;
import com.demo.seckill.common.enums.SeckillStatEnum;
import com.demo.seckill.common.redis.RedisUtils;
import com.demo.seckill.common.websocket.WebSocketServer;
import com.demo.seckill.service.ISeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author libo
 * @ClassName RedisConsumer
 * @Description: 消费消息
 * @date 2018/11/30 14:12
 */
@Service
public class RedisConsumer {

    @Autowired
    private ISeckillService seckillService;

    @Autowired
    private RedisUtils redisUtils;

    public void receiveMessage(String message){
        //收到通道的消息之后执行秒杀操作(超卖)
        String[] array = message.split(";");
        if(redisUtils.getValue(array[0])==null){//control层已经判断了，其实这里不需要再判断了
            Result result = seckillService.startSeckilDBPCC_TWO(Long.parseLong(array[0]), Long.parseLong(array[1]));
            if(result.equals(Result.ok(SeckillStatEnum.SUCCESS))){
                WebSocketServer.sendInfo(array[0].toString(), "秒杀成功");//推送给前台
            }else{
                WebSocketServer.sendInfo(array[0].toString(), "秒杀失败");//推送给前台
                redisUtils.cacheValue(array[0], "ok");//秒杀结束
            }
        }else{
            WebSocketServer.sendInfo(array[0].toString(), "秒杀失败");//推送给前台
        }
    }
}
